<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Exception;

class Qianzi extends Frontend
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';


    public function dian()
    {
        $id = $_GET['dian'];
        $this->assign('orderid', $id);
        if (empty($id)) {
            $this->error('请先选择要生成服务单的对象');
        } else {
            $res = Db::name('list_one')->where('id', $id)->find();
            // if ($res['kehu_sumdiscount']) {
            //     $res['main_sumdiscount'] = $res['kehu_sumdiscount'];
            // }
            $res['car_use_nature_str'] = $this->car_use_nature_str($res['car_use_nature']);

            $tc_list = array();
            $k       = 0;
            $line    = 8;
            if ($res['main_standard']) {
                $tc_list[$k]['tczl']  = '机动车车损损失';
                $tc_list[$k]['bjmp']  = $res['main_franchise'];
                $tc_list[$k]['money'] = $res['main_money'];
                $tc_list[$k]['tcf']   = number_format($res['main_submit'], 2);
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = number_format(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['main_standard'],2),100,4),2, '.', '');
//                }

                $k = $k + 1;
            }
            if ($res['main_biao']) {
                $tc_list[$k]['tczl']  = '机动车第三者责任';
                $tc_list[$k]['bjmp']  = $res['main_ji'];
                $tc_list[$k]['money'] = $res['main_santong'];
                $tc_list[$k]['tcf']   = number_format(floatval($res['main_yingjiao']), 2);
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = number_format(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['main_biao'],2),100,4),2, '.', '');
//                }
                $k = $k + 1;
            }
            if ($res['dao_biao']) {
                $tc_list[$k]['tczl']  = '医保外用药';
                $tc_list[$k]['bjmp']  = $res['dao_ji'];
                $tc_list[$k]['money'] = $res['dao_tong'];
                $tc_list[$k]['tcf']   = number_format($res['dao_ying'], 2);
                $k                    = $k + 1;
            }
            if ($res['siji_biao']) {
                $tc_list[$k]['tczl']  = '机动车车上人员-司机 *1';
                $tc_list[$k]['bjmp']  = $res['siji_ji'];
                $tc_list[$k]['money'] = $res['siji_tong'];
                // $tc_list[$k]['tcf']= $res['siji_ying'];
                $tc_list[$k]['tcf'] = number_format($res['siji_ying'], 2);
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = number_format(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['siji_biao'],2),100,4),2, '.', '');
//                }
                $k = $k + 1;
            }
            if ($res['ck_biao']) {
                $tc_list[$k]['tczl']  = '机动车车上人员-乘客 *' . (string)($res['car_carry_peo'] - 1);
                $tc_list[$k]['bjmp']  = $res['ck_ji'];
                $tc_list[$k]['money'] = $res['ck_tong'];
                $tc_list[$k]['tcf']   = number_format($res['ck_ying'], 2);
                $k                    = $k + 1;
            }

            if($res['dw_biao']){
                $tc_list[$k]['tczl']='外部电网故障损失险';
                $tc_list[$k]['bjmp'] = $res['dw_ji'];
                $tc_list[$k]['money']= $res['dw_tong'];
                $tc_list[$k]['tcf']= $res['dw_ying'];
                $k=$k+1;
            }
            $site = \think\Config::get('site');
            $res['chexing'] = array_flip($site['chexing'])[$res['chexing']];
            $line = $line - $k;
            $this->assign('tc_list', $tc_list);
            $this->assign('line', $line);
            $tiaokuan = db('tiaokuan')->where('gs_type', 1)->order('id desc')->limit(1)->find();
            $this->assign('tiaokuan', $tiaokuan);
            $this->assign('res', $res);
            $sign = Db::name('sign')->where('orderid', $id)->find();
            $this->assign('sign', $sign);
            // 临时关闭当前模板的布局功能
            $this->view->engine->layout(false);
            return $this->fetch('dian');
        }
    }


    public function dians()
    {
        $id = $_GET['dian'];
        $this->assign('orderid', $id);
        if (empty($id)) {
            $this->error('请先选择要生成服务单的对象');
        } else {
            $res = Db::name('list_one')->where('id', $id)->find();
            // if ($res['kehu_sumdiscount']) {
            //     $res['main_sumdiscount'] = $res['kehu_sumdiscount'];
            // }
            $res['car_use_nature_str'] = $this->car_use_nature_str($res['car_use_nature']);

            $tc_list = array();
            $k       = 0;
            $line    = 8;
            if ($res['main_standard']) {
                $tc_list[$k]['tczl']  = '机动车车损损失';
                $tc_list[$k]['bjmp']  = $res['main_franchise'];
                $tc_list[$k]['money'] = $res['main_money'];
                $tc_list[$k]['tcf']   = number_format($res['main_submit'], 2);
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = number_format(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['main_standard'],2),100,4),2, '.', '');
//                }

                $k = $k + 1;
            }
            if ($res['main_biao']) {
                $tc_list[$k]['tczl']  = '机动车第三者责任';
                $tc_list[$k]['bjmp']  = $res['main_ji'];
                $tc_list[$k]['money'] = $res['main_santong'];
                $tc_list[$k]['tcf']   = number_format($res['main_yingjiao'], 2);
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = number_format(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['main_biao'],2),100,4),2, '.', '');
//                }
                $k = $k + 1;
            }
            if ($res['dao_biao']) {
                $tc_list[$k]['tczl']  = '医保外用药';
                $tc_list[$k]['bjmp']  = $res['dao_ji'];
                $tc_list[$k]['money'] = $res['dao_tong'];
                $tc_list[$k]['tcf']   = number_format($res['dao_ying'], 2);
                $k                    = $k + 1;
            }
            if ($res['siji_biao']) {
                $tc_list[$k]['tczl']  = '机动车车上人员-司机 *1';
                $tc_list[$k]['bjmp']  = $res['siji_ji'];
                $tc_list[$k]['money'] = $res['siji_tong'];
                // $tc_list[$k]['tcf']= $res['siji_ying'];
                $tc_list[$k]['tcf'] = number_format($res['siji_ying'], 2);
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = number_format(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['siji_biao'],2),100,4),2, '.', '');
//                }
                $k = $k + 1;
            }
            if ($res['ck_biao']) {
                $tc_list[$k]['tczl']  = '机动车车上人员-乘客 *' . (string)($res['car_carry_peo'] - 1);
                $tc_list[$k]['bjmp']  = $res['ck_ji'];
                $tc_list[$k]['money'] = $res['ck_tong'];
                $tc_list[$k]['tcf']   = number_format($res['ck_ying'], 2);
                $k                    = $k + 1;
            }
            $site = \think\Config::get('site');
            $res['chexing'] = array_flip($site['chexing'])[$res['chexing']];
            $line = $line - $k;
            $this->assign('tc_list', $tc_list);
            $this->assign('line', $line);
            $tiaokuan = db('tiaokuan')->where('gs_type', 1)->order('id desc')->limit(1)->find();
            $this->assign('tiaokuan', $tiaokuan);
            $this->assign('res', $res);
            $sign = Db::name('sign')->where('orderid', $id)->find();
            $this->assign('sign', $sign);
            // 临时关闭当前模板的布局功能
            $this->view->engine->layout(false);
            return $this->fetch('dians');
        }
    }

    public function car_use_nature_str($value)
    {
        try {
            return model('Constants')->CAR_USE_NATURE[$value];
        } catch (Exception $e) {
            return '';
        }
    }

    public function imgbas()
    {
        $image     = input('post.file');
        $imageName = "25220_" . date("His", time()) . "_" . rand(1111, 9999) . '.png';
        if (strstr($image, ",")) {
            $image = explode(',', $image);
            $image = $image[1];
        }
        $path = "sign/" . date("Ymd", time());
        if (!is_dir($path)) { //判断目录是否存在 不存在就创建
            mkdir($path, 0777, true);
        }
        $imageSrc = $path . "/" . $imageName;                           //图片名字
        $r        = file_put_contents($imageSrc, base64_decode($image));//返回的是字节数
        if (!$r) {
            $tmparr1 = array('data' => null, "status" => 500, "msg" => "图片生成失败");
            return json($tmparr1);
        } else {
            $tmparr2 = array('data' => $imageSrc, "status" => 200, "msg" => "图片生成成功");
            return json($tmparr2);
        }
    }

    public function postsign()
    {
        $params = request()->param();
        $data   = array(
            'orderid'   => $params['id'],
            'sign_pic'  => $params['urls'],
            'sign_time' => date('Y-m-d H:i:s')
        );
        $issign = Db::name('sign')->where('orderid', $params['id'])->find();
        if ($issign) {
            return json(['status' => 200, 'msg' => '你已经签字了']);
        }
        $res = Db::name('sign')->insert($data);
        if ($res) {
            return json(['status' => 200, 'msg' => '签字成功']);
        } else {
            return json(['status' => 500, 'msg' => '签字失败']);
        }
    }
    public function cha()
    {
        // 临时关闭当前模板的布局功能
        $this->view->engine->layout(false);
        return $this->fetch("cha");
    }
    public function cha_post()
    {
        $param = $_POST;
        $res = Db::name('list_one')
            ->where(['car_num'=>$param['car_num'],'status'=>5])
            ->where("RIGHT(car_vin, 4) LIKE '%{$param['car_vin']}'")
            ->field('id')
            ->find();
        // ->select(false);
        // dump($res);die;
        if(!$res){
            return json(['code'=>-1,'msg'=>'查询失败']);
        }
        //查询是否退统
        $status_tui = db('list_three')->where('ids',$res['id'])->where(['batch_state'=>1])->field('id')->find();
        if($status_tui){
            return json(['code'=>-1,'msg'=>'查询失败1']);
        }
        return json(['code'=>1,'msg'=>'查询成功']);
    }
    public function cha_t()
    {
        $param = $_GET;
        // $this->assign('orderid',$id);
        if(empty($param)){
            $this->error('请先选择要查询的服务单的对象');
        }else{
            $res = Db::name('list_one')
                ->where(['car_num'=>$param['car_num'],'status'=>5])
                ->where("RIGHT(car_vin, 4) LIKE '%{$param['car_vin']}'")
                ->field('id')
                ->find();

            if(!$res){
                $this->error('查询失败');
            }
            $res = Db::name('list_one')->where(['id'=>$res['id']])->find();
            // if ($res['kehu_sumdiscount']) {
            //     $res['main_sumdiscount'] = $res['kehu_sumdiscount'];
            // }
            $res['car_use_nature_str'] = $this->car_use_nature_str($res['car_use_nature']);

            $tc_list = array();
            $k       = 0;
            $line    = 8;
            if ($res['main_standard']) {
                $tc_list[$k]['tczl']  = '机动车车损损失';
                $tc_list[$k]['bjmp']  = $res['main_franchise'];
                $tc_list[$k]['money'] = $res['main_money'];
                $tc_list[$k]['tcf']   = number_format($res['main_submit'], 2);
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = number_format(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['main_standard'],2),100,4),2, '.', '');
//                }

                $k = $k + 1;
            }
            if ($res['main_biao']) {
                $tc_list[$k]['tczl']  = '机动车第三者责任';
                $tc_list[$k]['bjmp']  = $res['main_ji'];
                $tc_list[$k]['money'] = $res['main_santong'];
                $tc_list[$k]['tcf']   = number_format($res['main_yingjiao'], 2);
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = number_format(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['main_biao'],2),100,4),2, '.', '');
//                }
                $k = $k + 1;
            }
            if ($res['dao_biao']) {
                $tc_list[$k]['tczl']  = '医保外用药';
                $tc_list[$k]['bjmp']  = $res['dao_ji'];
                $tc_list[$k]['money'] = $res['dao_tong'];
                $tc_list[$k]['tcf']   = number_format($res['dao_ying'], 2);
                $k                    = $k + 1;
            }
            if ($res['siji_biao']) {
                $tc_list[$k]['tczl']  = '机动车车上人员-司机 *1';
                $tc_list[$k]['bjmp']  = $res['siji_ji'];
                $tc_list[$k]['money'] = $res['siji_tong'];
                // $tc_list[$k]['tcf']= $res['siji_ying'];
                $tc_list[$k]['tcf'] = number_format($res['siji_ying'], 2);
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = number_format(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['siji_biao'],2),100,4),2, '.', '');
//                }
                $k = $k + 1;
            }
            if ($res['ck_biao']) {
                $tc_list[$k]['tczl']  = '机动车车上人员-乘客 *' . (string)($res['car_carry_peo'] - 1);
                $tc_list[$k]['bjmp']  = $res['ck_ji'];
                $tc_list[$k]['money'] = $res['ck_tong'];
                $tc_list[$k]['tcf']   = number_format($res['ck_ying'], 2);
                $k                    = $k + 1;
            }

            if($res['dw_biao']){
                $tc_list[$k]['tczl']='外部电网故障损失险';
                $tc_list[$k]['bjmp'] = $res['dw_ji'];
                $tc_list[$k]['money']= $res['dw_tong'];
                $tc_list[$k]['tcf']= $res['dw_ying'];
                $k=$k+1;
            }
            $site = \think\Config::get('site');
            $site['chexing']['6座以下.'] = '6座以下';
            $site['chexing']['特二.'] = '特二';
            $site['chexing']['6-10座.'] = '6-10座';
            $res['chexing'] = array_flip($site['chexing'])[$res['chexing']];
            $line = $line - $k;
            $this->assign('tc_list', $tc_list);
            $this->assign('line', $line);
            $tiaokuan = db('tiaokuan')->order('id desc')->limit(1)->find();
            $this->assign('tiaokuan', $tiaokuan);
            $this->assign('res', $res);
            $sign = Db::name('sign')->where('orderid', $res['id'])->find();
            $this->assign('sign', $sign);

            $this->assign('orderid',$res['id']);
            // 临时关闭当前模板的布局功能
            $this->view->engine->layout(false);

            return $this->fetch('cha_t');
        }
    }
}